﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using Lon.Server;
using Lon.DataStore;
using Lon.Util;


namespace Lon.UI
{
    public partial class FormNetLogView : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        private DevAge.ComponentModel.BoundList<ConnectLogTable> mBoundList;
        List<ConnectLogTable> recs;
     
        IDataAccessService dataAccessService = DataAccessSeriveFactory.GetService(null);
        public FormNetLogView()
        {
            InitializeComponent();
            recs=dataAccessService.ReadConnectLog(DateTimeHelper.DateTime2UnixTime(DateTime.Now.Date),
                                             DateTimeHelper.DateTime2UnixTime(DateTime.Now.Date.AddDays(1)));
            InitList(recs);
            CreateColumns(mBoundList);
        }

        private void InitList(List<ConnectLogTable> recs)
        {

            mBoundList = new DevAge.ComponentModel.BoundList<ConnectLogTable>(recs);
            mBoundList.AllowNew = false;
            mBoundList.AllowDelete = false;
            mBoundList.AllowEdit = false;
         
            
          
        }
        private void CreateColumns(DevAge.ComponentModel.IBoundList boundList)
        {

            //Create columns
            SourceGrid.DataGridColumn gridColumn;
        
          
            SourceGrid.Cells.Controllers.ToolTipText toolTip=   new SourceGrid.Cells.Controllers.ToolTipText();
            toolTip.ToolTipTitle = "???";
            toolTip.ToolTipIcon = ToolTipIcon.Info;
            toolTip.IsBalloon = true;
            gridColumn = dataGrid.Columns.Add("StationName", "站名", typeof(String));
            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;


            gridColumn = dataGrid.Columns.Add("LogTime", "时间", new UnixTimeEditor());
            gridColumn.AutoSizeMode =  SourceGrid.AutoSizeMode.EnableAutoSize;
 
            gridColumn = dataGrid.Columns.Add("LogMessage", "状态改变信息", typeof(string));
            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableStretch;
            gridColumn.DataCell.AddController(toolTip);
            
               
            
 
            dataGrid.AutoSize = true;
            dataGrid.DataSource = mBoundList;
            dataGrid.AutoSizeCells();
            dataGrid.AutoStretchColumnsToFitWidth = true;
            dataGrid.Columns.StretchToFit();
            
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
          
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = this.dateTimePicker1.Value;
            recs = dataAccessService.ReadConnectLog(DateTimeHelper.DateTime2UnixTime(dt.Date),
                                           DateTimeHelper.DateTime2UnixTime(dt.Date.AddDays(1)));
            InitList(recs);
            dataGrid.DataSource = mBoundList;
            this.dataGrid.Invalidate();
        }
        
    }

    public class UnixTimeEditor : SourceGrid.Cells.Editors.EditorBase
    {
        public UnixTimeEditor()
            : base(null)
        {
 
        }
        public override string ValueToDisplayString(object val)
        {
            if (val == null) return "";
            try
            {
                DateTime dt=DateTimeHelper.UnixTime2DateTime((UInt32)val);
                String ret=dt.ToString("HH:mm:ss");
                return ret;
            }
            catch(Exception ex)
            {
                return ex.ToString();
            }
        }
    }

}
